#include "Base.h"
#include "ODBCBase.h"

CODBCBase::CODBCBase()
{
	m_nResult = 0;
	m_nResultCount = 0;
	m_pInterface = NULL;
}

void CODBCBase::SetODBCInterface(CODBCInterface *pInterface)
{
	m_pInterface = pInterface; 
}

UINT CODBCBase::GetResultCount()
{
	return m_nResultCount;
}

INT	CODBCBase::GetInternalAffectCount()
{
	return m_pInterface->GetAffectedRowCount();
}

BOOL   CODBCBase::IsConnected()
{
	if(m_pInterface == NULL)
	{
		return FALSE;
	}
	return m_pInterface->IsConnected();
}

BOOL CODBCBase::IsPrepared()
{
	if(m_pInterface == NULL)
	{
		return FALSE;
	}
	return m_pInterface->IsPrepare();
}

BOOL CODBCBase::Load()
{
	if(!IsConnected())
		return FALSE;
	m_nOPType = DB_LOAD;
	m_nResult = m_pInterface->Execute();
	m_nResultCount	= m_pInterface->m_nAffectCount;
	return m_nResult;
}

BOOL CODBCBase::AddNew()
{
	if(!IsConnected())
		return FALSE;
	m_nOPType = DB_ADDNEW;
	m_nResult = m_pInterface->Execute();
	m_nResultCount	= m_pInterface->m_nAffectCount;
	return m_nResult;
}

BOOL CODBCBase::Delete()
{
    if(!IsConnected())
		return FALSE;
	m_nOPType = DB_DELETE;
	m_nResult = m_pInterface->Execute();
	m_nResultCount	= m_pInterface->m_nAffectCount;
	return m_nResult;
}

BOOL CODBCBase::Prepare()
{
	if(!IsConnected())
		return FALSE;
	return m_pInterface->ExecutePrepare();
}

BOOL CODBCBase::Save(VOID* pSource)
{
	if(!IsConnected())
		return FALSE;
	m_nOPType = DB_SAVE;
	m_nResult = m_pInterface->ExecuteProc();
	m_nResultCount	= m_pInterface->m_nAffectCount;
	return m_nResult;

}

INT	CODBCBase::GetErrorCode()
{
	return m_pInterface->GetError();
}

CHAR* CODBCBase::GetErrorMessage()
{
	return m_pInterface->GetErrorMsg();
}

void CODBCBase::BindWord(int nPos, int nType, WORD *pWordParam)
{
	m_cbValue[nPos] = 0;
    SQLBindParameter(m_pInterface->m_hStmt,nPos,nType,SQL_C_USHORT,SQL_SMALLINT,0,0,pWordParam,0,&m_cbValue[nPos]);
}

void CODBCBase::BindInt(int nPos, int nType, int *pIntParam)
{
	m_cbValue[nPos] = 0;
	int nRet = SQLBindParameter(m_pInterface->m_hStmt,nPos,nType,SQL_C_SLONG,SQL_INTEGER,0,0,pIntParam,0,&m_cbValue[nPos]);
}

void CODBCBase::BindStr(int nPos, int nType, char *pStrParam)
{
	int nLen = strlen(pStrParam);
	m_cbValue[nPos] = SQL_NTS;
	int nRet = SQLBindParameter(m_pInterface->m_hStmt,nPos,nType,SQL_C_CHAR,SQL_VARCHAR,nLen,0,pStrParam,0,&m_cbValue[nPos]);
}

void CODBCBase::BindBinary(int nPos, int nType, void *pBinaryData, int nLen)
{
	m_cbValue[nPos] = SQL_NTS;
	SQLBindParameter(m_pInterface->m_hStmt,nPos,nType,SQL_C_BINARY,SQL_LONGVARBINARY,nLen,0,pBinaryData,nLen,&m_cbValue[nPos]);
}